#/usr/bin/env bash


ip=$(ip a | grep -E "10.251" | awk -F " |/" '{print $6}')
upuser="root"                                    # 程序账号
jdk_bin="/usr/java/jdk1.8.0_202-amd64/bin"
jmap="${jdk_bin}/jmap"             # jmap命令路径   
jstat="${jdk_bin}/jstat"           # jstat命令路径  
jstack="${jdk_bin}/jstack"         # jstack命令路径  
sysname="lhxc"

#________________________________脚本主体___________________________________________________


  collect_info(){
    [ $2 -gt 1024 ]  && upuser="root" || upuser="root"
  	mkdir -p  /tmp/${sysname}/tomcat
  	touch $5 && chmod 777 $5

    PID=$(ps -ef | grep "$1" | grep -v grep | awk '{print $2}')

    #JVM堆内存使用率检测？？
	ps -ef | grep "$PID" | grep -v grep | grep -Eio "\-Xmx[0-9]+[mg]" | grep -Eiq  m$   &&  unit=1048576     || unit=1262485504
    #echo -e "JVM堆内存使用率 $(echo "scale=2;$(echo "scale=2;$(sudo -u $upuser $jmap -histo $PID | grep -w Total | awk '{print $3}') / $unit  "|bc) * 100 / $(ps -ef | grep "$PID" | grep -v grep | grep -Eio "\-Xmx[0-9]+[mg]" | grep -Eo "[0-9]+")"|bc)%" >   $5
	 echo -e "JVM堆内存使用率 $(echo "scale=2;$(echo "scale=2;$(sudo -vu os_admin $jmap -histo $PID | grep -w Total | awk '{print $3}')" | grep -Eo "[0-9]+")"|bc)%"   >   $5
        #echo -e "JVM堆内存使用率 37%" >>   $5
    #Tomcat进程检测
	  echo -e "进程状态 $(ps -ef | grep -v grep | grep "$1" &> /dev/null && echo "1" || echo "0")"  >>  $5

    #Tomcat端口检测
	  echo -e "进程端口状态 $(sudo -u $upuser netstat -anptl | grep $PID | grep LISTEN | grep -w $2 &> /dev/null && echo "1" || echo "0")"  >>   $5

	  
#屏蔽20220510
    #Tomcat FGC频次检测 + 次数检查 + 时间检查 + 新生代使用率 + 老年代使用率
    if [ $(sudo -u $upuser $jstat -gcutil $PID 12000 5 2> /dev/null | awk  '{print $9}'  | grep -iow FGC | wc -l ) == 1 ];then
	#    echo -e "FGC频次是否异常(1Min) $(sudo -u $upuser $jstat -gcutil $PID 12000 5 2> /dev/null | sed -n "2p;\$p" | awk '{if(NR==1){a=$9}else{b=$9}}END{if(b>a){print "0"}else{print "1"};{print "FGC总次数",$9};{print "FGC总用时(s)",$10};{print "JVM年轻代内存使用率(%)",$3};{print "JVM年老代内存使用率(%)",$4}}')"    >>   $5
		echo -e "FGC频次是否异常(1Min) $(sudo -u $upuser $jstat -gcutil $PID 12000 5 2> /dev/null | sed -n "2p;\$p" | awk '{if(NR==1){a=$9}else{b=$9}}END{if(b>a){print "0"}else{print "1"};{print "FGC总次数",9};{print "FGC总用时(s)",10};{print "JVM年轻代内存使用率(%)",3};{print "JVM年老代内存使用率(%)",4}}')"   >>   $5

    else
    #  echo -e "FGC频次是否异常(1Min) $(sudo -u $upuser $jstat -gcutil $PID 12000 5 2> /dev/null | sed -n "2p;\$p" | awk '{if(NR==1){a=$8}else{b=$8}}END{if(b>a){print "0"}else{print "1"};{print "FGC总次数",$8};{print "FGC总用时(s)",$9};{print "JVM年轻代内存使用率(%)",$3};{print "JVM年老代内存使用率(%)",$4}}')"     >>   $5
	echo -e "FGC频次是否异常(1Min) $(sudo -u $upuser $jstat -gcutil $PID 12000 5 2> /dev/null | sed -n "2p;\$p" | awk '{if(NR==1){a=$8}else{b=$8}}END{if(b>a){print "0"}else{print "1"};{print "FGC总次数",8};{print "FGC总用时(s)",9};{print "JVM年轻代内存使用率(%)",3};{print "JVM年老代内存使用率(%)",4}}')"      >>   $5
   fi

#echo -e "JVM年轻代内存使用率 37%" >>   $5
#echo -e "JVM年老代内存使用 15%" >>   $5


    #Tomcat活动线程数 + 占比检测
	  echo -e "活动线程数 $(sudo -u $upuser ps -T -p $PID | grep -v grep | wc -l)\n活动线程数占比 $(echo "scale=2;$(sudo -u $upuser ps -T -p $PID | grep -v grep | wc -l) * 100 / 500"|bc)%"   >>   $5

    #Tomcat线程状态
    Thread_info=$(sudo -u $upuser $jstack $PID | grep "java.lang.Thread.State" | awk '{print $2}')
    for state in NEW RUNNABLE BLOCKED WAITING TIMED_WAITING TERMINATED
    do
      echo -e "线程状态($state) $(echo $Thread_info | grep -o $state | wc -l)"   >>   $5
    done

    #Tomcat JDBC连接池活动数占比检测
	  echo -e "JDBC连接池活动数占比 $(echo "scale=2;$(sudo -u $upuser netstat -anptl | grep $PID | grep "3306" | grep -c "ESTABLISHED") * 100 / 40"|bc)%"   >>   $5

    #Tomcat应用日志检测
    sudo -u $upuser awk  "/$(date -d "6 minutes ago" "+%H:%M")/,/$(date "+%H:%M")/"  $3  >  /tmp/${sysname}/tomcat/logs_info.txt
	for word in WARN ERROR OutOfMemory JDBCException IOException SocketException
	do
	    echo -e "${word}异常日志 $( grep -oi $word /tmp/${sysname}/tomcat/logs_info.txt | wc -l)"  >>   $5
	done

    #Tomcat访问日志检测
	
    sudo -u $upuser awk  "/$(date -d "6 minutes ago" "+%H:%M")/,/$(date "+%H:%M")/" $4 2> /dev/null | grep -Ewo  " [45]0[0-9] "   > /tmp/${sysname}/tomcat/code_info.txt
    for code in 40{0..5} 50{0..5}
    do
	    echo -e "异常状态码${code} $(grep -o $code /tmp/${sysname}/tomcat/code_info.txt  | wc -l)"  >>   $5
	done
	
	#远程发送
	ssh   toptea@10.253.55.32   "mkdir  -p /tmp/${sysname}.zd/tomcat"
	scp $5 toptea@10.253.55.32:/tmp/${sysname}.zd/tomcat && echo "File: $5 send to the directory of host '10.253.55.32' '/tmp/${sysname}.zd/Tomcat/'"  
	echo $5
	}
#--------------应用---------------------------	
#集客应用
                  
	app_log="/lcms/apache-tomcat-7.0.94-person/logs/catalina.out"       # catalina.out日志路径
	acc_log="/lcms/apache-tomcat-7.0.94-person/logs/localhost_access_log.*$(date +%F).txt"       # 日志路径
	
	collect_log="/tmp/${sysname}/tomcat/${ip}_${port}.txt"                # 文件命名        
	collect_info $server_name  $port  $app_log   $acc_log  $collect_log   &
	



sleep   10